package com.myd.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.myd.model.StandardData;
import com.myd.service.StandardDataService;
import com.myd.util.DownloadExcel;
import com.myd.vo.Result;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Controller
@Path("standard_data")
public class StandardDataController {


    @Autowired
    private StandardDataService standardDataService;
    @POST
    @Path("/insert")
    @Produces(MediaType.APPLICATION_JSON)
    public boolean insertData(@RequestBody StandardData standardData){
        System.out.println(standardData);
        return standardDataService.insertData(standardData);
    }

    @POST
    @Path("/add_excel_data")
    @Produces(MediaType.APPLICATION_JSON)
    public Result insertExcelData(@Context HttpServletRequest request) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }
        // 将资料解码
        String reqBody = sb.toString();
        String decode = URLDecoder.decode(reqBody, "UTF-8");
        List<StandardData> list = new ArrayList<>();
        list = (List<StandardData>) JSON.parse(decode);
        boolean b = standardDataService.inserExcelData(list);
        return Result.success();
    }

    @GET
    @Path("/search/{station_no}")
    public String searchByStation_no(@PathParam("station_no")int station_no){
        List<StandardData> list = standardDataService.searchByStation_no(station_no);
        Result success = Result.success();
        success.setObject(list);
//        JSONObject jsonObject = new JSONObject();
        String s = JSON.toJSONString(success);
        return s;
    }

    @GET
    @Path("/searchByTime/{station_no}/{year}")
    public String searchByTime(@PathParam("station_no")String station,@PathParam("year")String year){
        List<StandardData> list = standardDataService.searchByTime(station,year);
        Result result = Result.success();
        result.setObject(list);
        String s = JSON.toJSONString(result);
        return s;
    }



    @GET
    @Path("/download/{year}")
    public void downloadExcel(@PathParam("year") int year,
                              @Context HttpServletRequest request, @Context HttpServletResponse response){
        String filename = "标准气象年数据.xlsx";
        List<StandardData> list = standardDataService.selectList(year);
        XSSFWorkbook excel = DownloadExcel.downloadStandardExcel("标准气象年数据", list);
        OutputStream outputStream = null;

        try {
            String headStr = "attachment; filename=\"" + new String(filename.getBytes("utf-8"), "ISO8859-1" ) + "\"";
            response.setHeader("Content-Disposition",headStr);
            response.setContentType("application/octet-stream");
            outputStream=response.getOutputStream();
            excel.write(outputStream);
            assert outputStream != null;
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
